package com.anytron.base.service.impl;

import java.util.List;

import com.anytron.base.mapper.BaseMapper;
import com.anytron.base.service.BaseService;
import com.anytron.model.req.PageRequest;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import tk.mybatis.mapper.entity.Example;

public class BaseServiceImpl<T>  implements BaseService<T> {

	public BaseMapper<T> mapper;
	
	@Override
	public PageInfo<T> page(PageRequest pageRequest, T bean) {
		Page<T> startPage = PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
		mapper.select(bean);
		return startPage.toPageInfo();
	}

	@Override
	public List<T> list(T bean) {
		return mapper.select(bean);
	}

	@Override
	public T getByKey(Object key) {
		return mapper.selectByPrimaryKey(key);
	}

	@Override
	public int add(T bean) {
		return mapper.insertSelective(bean);
	}

	@Override
	public int update(T bean) {
		return mapper.updateByPrimaryKeySelective(bean);
	}

	@Override
	public int delete(Object key) {
		return mapper.deleteByPrimaryKey(key);
	}

	@Override
	public List<T> getByExample(Example example) {
		return mapper.selectByExample(example);
	}

	@Override
	public PageInfo<T> pageByExample(PageRequest pageRequest, Example example) {
		Page<T> startPage = PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
		mapper.selectByExample(example);
		return startPage.toPageInfo();
	}

	@Override
	public T getOne(T bean) {
		return mapper.selectOne(bean);
	}

	@Override
	public int deleteByModel(T bean) {
		return mapper.delete(bean);
	}

}
